package com.freepbx.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;

import lombok.Cleanup;

/**
 * @author litong
 * @date 2018年6月4日_下午4:02:31 
 * @version 1.0 
 */
public class AddUsers {

	/**
	 * freepbx添加用户名和密码,密码是 xxxaa
	 * @param start
	 * @param end
	 */

	public static void addUsers(int start, int end) {
		CloseableHttpClient httpClient = HttpClients.createDefault();
		HttpPost httpPost = new HttpPost("http://139.159.0.114/admin/config.php");

		for (int i = start; i <= end; i++) {
			List<NameValuePair> params = new ArrayList<>();

			params.add(new BasicNameValuePair("display", "extensions"));
			params.add(new BasicNameValuePair("type", ""));
			params.add(new BasicNameValuePair("action", "add"));
			params.add(new BasicNameValuePair("extdisplay", ""));
			params.add(new BasicNameValuePair("action", "add"));
			params.add(new BasicNameValuePair("extdisplay", ""));
			params.add(new BasicNameValuePair("extension", i + ""));
			params.add(new BasicNameValuePair("name", i + ""));
			params.add(new BasicNameValuePair("cid_masquerade", ""));
			params.add(new BasicNameValuePair("sipname", ""));
			params.add(new BasicNameValuePair("outboundcid", ""));
			params.add(new BasicNameValuePair("ringtimer", "0"));
			params.add(new BasicNameValuePair("cfringtimer", "0"));
			params.add(new BasicNameValuePair("concurrency_limit", "0"));
			params.add(new BasicNameValuePair("callwaiting", "enabled"));
			params.add(new BasicNameValuePair("call_screen", "0"));
			params.add(new BasicNameValuePair("pinless", "disabled"));
			params.add(new BasicNameValuePair("emergency_cid", ""));
			params.add(new BasicNameValuePair("tech", "disabled"));
			params.add(new BasicNameValuePair("pinless", "sip"));
			params.add(new BasicNameValuePair("hardware", "generic"));
			params.add(new BasicNameValuePair("newdid_name", ""));
			params.add(new BasicNameValuePair("newdid", ""));
			params.add(new BasicNameValuePair("newdidcid", ""));
			params.add(new BasicNameValuePair("devinfo_secret_origional", ""));
			params.add(new BasicNameValuePair("devinfo_dtmfmode", "rfc2833"));
			params.add(new BasicNameValuePair("devinfo_canreinvite", "no"));
			params.add(new BasicNameValuePair("devinfo_context", "internal"));
			params.add(new BasicNameValuePair("devinfo_host", "dynamic"));
			params.add(new BasicNameValuePair("devinfo_trustrpid", "yes"));
			params.add(new BasicNameValuePair("devinfo_sendrpid", "no"));
			params.add(new BasicNameValuePair("devinfo_type", "friend"));
			params.add(new BasicNameValuePair("devinfo_nat", "yes"));
			params.add(new BasicNameValuePair("devinfo_port", "5060"));
			params.add(new BasicNameValuePair("devinfo_qualify", "60"));
			params.add(new BasicNameValuePair("devinfo_transport", "udp"));
			params.add(new BasicNameValuePair("devinfo_encryption", "no"));
			params.add(new BasicNameValuePair("devinfo_callgroup", ""));
			params.add(new BasicNameValuePair("devinfo_pickupgroup", ""));
			params.add(new BasicNameValuePair("devinfo_disallow", ""));
			params.add(new BasicNameValuePair("devinfo_allow", ""));
			params.add(new BasicNameValuePair("devinfo_dial", ""));
			params.add(new BasicNameValuePair("devinfo_accountcode", ""));
			params.add(new BasicNameValuePair("devinfo_mailbox", ""));
			params.add(new BasicNameValuePair("devinfo_vmexten", ""));
			params.add(new BasicNameValuePair("devinfo_deny", "0.0.0.0/0.0.0.0"));
			params.add(new BasicNameValuePair("devinfo_vmexten", ""));
			params.add(new BasicNameValuePair("devinfo_permit", "0.0.0.0/0.0.0.0"));
			params.add(new BasicNameValuePair("noanswer_dest", "goto0"));
			params.add(new BasicNameValuePair("busy_dest", "goto1"));
			params.add(new BasicNameValuePair("chanunavail_dest", "goto2"));
			params.add(new BasicNameValuePair("cc_agent_policy", "generic"));
			params.add(new BasicNameValuePair("cc_monitor_policy", "generic"));
			params.add(new BasicNameValuePair("recording_in_external", "recording_in_external=always"));
			params.add(new BasicNameValuePair("recording_out_external", "recording_out_external=always"));
			params.add(new BasicNameValuePair("recording_in_internal", "recording_in_internal=always"));
			params.add(new BasicNameValuePair("recording_out_internal", "recording_out_internal=always"));
			params.add(new BasicNameValuePair("recording_ondemand", "disabled"));
			params.add(new BasicNameValuePair("recording_priority", "10"));
			params.add(new BasicNameValuePair("vm", "disabled"));
			params.add(new BasicNameValuePair("cxpanel_add_extension", "1"));
			params.add(new BasicNameValuePair("cxpanel_auto_answer", "0"));
			params.add(new BasicNameValuePair("cxpanel_add_user", "1"));
			params.add(new BasicNameValuePair("cxpanel_full_user", "1"));
			params.add(new BasicNameValuePair("cxpanel_email_new_pass", "0"));
			params.add(new BasicNameValuePair("cxpanel_password", i + "aa"));
			params.add(new BasicNameValuePair("cxpanel_extensions[]", "self"));
			params.add(new BasicNameValuePair("cxpanel_phone_numbers-number", ""));
			params.add(new BasicNameValuePair("cxpanel_phone_numbers-type", ""));
			params.add(new BasicNameValuePair("goto0", ""));
			params.add(new BasicNameValuePair("Announcements0", "app-announcement-2,s,1"));
			params.add(new BasicNameValuePair("Call_Flow_Control0", "popover"));
			params.add(new BasicNameValuePair("Call_Recording0", "popover"));
			params.add(new BasicNameValuePair("Conferences0", "popover"));
			params.add(new BasicNameValuePair("Custom_Applications0", "popover"));
			params.add(new BasicNameValuePair("Directory0", "popover"));
			params.add(new BasicNameValuePair("Extensions0", "from-did-direct,"));
			params.add(new BasicNameValuePair("Feature_Code_Admin0", "ext-featurecodes,*30,1"));
			params.add(new BasicNameValuePair("IVR0", "ivr-1,s,1"));
			params.add(new BasicNameValuePair("Terminate_Call0", "app-blackhole,hangup,1"));
			params.add(new BasicNameValuePair("Trunks0", "ext-trunk,1,1"));
			params.add(new BasicNameValuePair("noanswer_cid", ""));
			params.add(new BasicNameValuePair("goto1", ""));
			params.add(new BasicNameValuePair("Announcements1", "app-announcement-2,s,1"));
			params.add(new BasicNameValuePair("Call_Flow_Control1", "popover"));
			params.add(new BasicNameValuePair("Call_Recording1", "popover"));
			params.add(new BasicNameValuePair("Conferences1", "popover"));
			params.add(new BasicNameValuePair("Custom_Applications1", "popover"));
			params.add(new BasicNameValuePair("Directory1", "popover"));
			params.add(new BasicNameValuePair("Extensions1", "from-did-direct,2000,1"));
			params.add(new BasicNameValuePair("Feature_Code_Admin1", "ext-featurecodes,*30,1"));
			params.add(new BasicNameValuePair("IVR1", "ivr-1,s,1"));
			params.add(new BasicNameValuePair("Terminate_Call1", "app-blackhole,hangup,1"));
			params.add(new BasicNameValuePair("Trunks1", "ext-trunk,1,1"));
			params.add(new BasicNameValuePair("busy_cid", ""));
			params.add(new BasicNameValuePair("goto2", ""));
			params.add(new BasicNameValuePair("Announcements2", "app-announcement-2,s,1"));
			params.add(new BasicNameValuePair("Call_Flow_Control2", "popover"));
			params.add(new BasicNameValuePair("Call_Recording2", "popover"));
			params.add(new BasicNameValuePair("Conferences2", "popover"));
			params.add(new BasicNameValuePair("Custom_Applications2", "popover"));
			params.add(new BasicNameValuePair("Directory2", "popover"));
			params.add(new BasicNameValuePair("Extensions2", "from-did-direct,2000,1"));
			params.add(new BasicNameValuePair("Feature_Code_Admin2", "ext-featurecodes,*30,1"));
			params.add(new BasicNameValuePair("IVR2", "ivr-1,s,1"));
			params.add(new BasicNameValuePair("Terminate_Call2", "app-blackhole,hangup,1"));
			params.add(new BasicNameValuePair("Trunks2", "ext-trunk,1,1"));
			params.add(new BasicNameValuePair("chanunavail_cid", ""));
			params.add(new BasicNameValuePair("Submit", "Submit"));

			HttpEntity httpEntity = null;
			try {
				httpEntity = new UrlEncodedFormEntity(params);
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			httpPost.setEntity(httpEntity);

			CloseableHttpResponse httpResponse = null;
			try {
				httpResponse = httpClient.execute(httpPost);
			} catch (IOException e) {
				e.printStackTrace();
			}

			InputStream content = null;
			try {
				content = httpResponse.getEntity().getContent();
			} catch (UnsupportedOperationException | IOException e) {
				e.printStackTrace();
			}

			try {
				@Cleanup
				BufferedReader bufReader = new BufferedReader(new InputStreamReader(content));
				String line = null;
				while ((line = bufReader.readLine()) != null) {
					System.out.println(line);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

}
